package com.example.spzx.Service.impl;

import com.example.spzx.Mapper.BrandMapper;
import com.example.spzx.Mapper.CategoryMapper;
import com.example.spzx.Mapper.ProductMapper;
import com.example.spzx.Service.ProductService;
import com.example.spzx.model.dto.product.ProductDto;
import com.example.spzx.model.entity.product.Product;
import com.example.spzx.model.entity.product.ProductDetails;
import com.example.spzx.model.entity.product.ProductSku;
import com.example.spzx.model.entity.product.ProductSpec;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
  @Resource
  private BrandMapper brandMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ProductMapper productMapper;
    public PageInfo<Product> select(Integer page, Integer limit, ProductDto productDto){
        PageHelper.startPage(page,limit);
        Page<Product> page1=productMapper.getpage(productDto);
        PageInfo<Product> pageInfo=new PageInfo<>();
        pageInfo.setList(page1.getResult());
        pageInfo.setTotal(page1.getTotal());
        return pageInfo;
    }
    @Transactional
    public     void save(Product product){
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.save(product);

        List<ProductSku> list=product.getProductSkuList();
        for (ProductSku productSku:list){

            productSku.setSaleNum(0);
            productSku.setProductId(product.getId());

            productMapper.savesku(productSku);
        }
        ProductDetails productDetails=new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productMapper.saveProductDetail(productDetails);




    }
    public Product getbyid(Integer id){
        Product product=productMapper.getbyid(id);
        List<ProductSku> list=productMapper.getskubyid(id);
        product.setProductSkuList(list);
product.setBrandName(brandMapper.getnamebyid(Math.toIntExact(product.getBrandId())));

        product.setCategory1Name(categoryMapper.selectbyid(product.getCategory1Id()).getName());
        product.setCategory2Name(categoryMapper.selectbyid(product.getCategory2Id()).getName());
        product.setCategory3Name(categoryMapper.selectbyid(product.getCategory3Id()).getName());
        ProductDetails productDetails=productMapper.selectbyid(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;

    }
    @Transactional
    public     void updateByid(Product product){
        productMapper.updateByid(product);
        List<ProductSku> list=productMapper.getskubyid(Math.toIntExact(product.getId()));
        if (list.size()>0){
            productMapper.deleteskuByid(product.getId());
        }
        List<ProductSku> list1=product.getProductSkuList();
        for (ProductSku productSku:list1){

            productSku.setSaleNum(0);
            productSku.setProductId(product.getId());

            productMapper.savesku(productSku);
        }
        ProductDetails productDetails1=productMapper.selectbyid(product.getId());
        if(productDetails1 == null){
            ProductDetails productDetails=new ProductDetails();
            productDetails.setProductId(product.getId());
            productDetails.setImageUrls(product.getDetailsImageUrls());
            productMapper.saveProductDetail(productDetails);
        }else{

            productDetails1.setProductId(product.getId());
            productDetails1.setImageUrls(product.getDetailsImageUrls());
            productMapper.updatedetail(productDetails1);
        }


    }
}
